<?php
/**
 * @author guoxiaoyong
 */
namespace frontend\controllers;

use Yii;
use bc\controller\FrontController;
use bc\wx\Auth;
use bc\util\Util;
use bc\wx\User;
use bc\wx\OAuth;

/**
 * Site controller
 */
class SiteController extends FrontController
{
	public function actionIndex()
	{
		return $this->render('index');
	}
	
	
	public function actionTest()
	{
		$openid = $this->get('openid');
		
		if($openid)
		{
			$view = [];
			$appid = $this->wechat()->appid;
			$secret = $this->wechat()->secret;
			
			$auth = new Auth($appid, $secret);
			$accessToken = $auth->accessToken;
			
			$user = new User();
			
			$info = $user->getUserInfo($accessToken, $openid);
			echo $info['nickname'].'<br/>';
			echo '<img src="' . $info['headimgurl']  .'"><br/>';
			echo date('Y-m-d H:i:s', $info['subscribe_time']);
			exit;
		}
		
		
	}
	
	public function actionScan()
	{
		$oauth = new OAuth();
		$redirectUri = 'http://wx.zhishimama.com/site/code';
		$rs = $oauth->authorize($redirectUri, OAuth::SNSAPI_BASE);
		print_r($rs);
	}
	
	
	public function actionCode()
	{
		$code = $this->get('code');
		
		if($code)
		{
			$appid = $this->wechat()->appid;
			$secret = $this->wechat()->secret;
			$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code";
			$rs = Util::httpGet($url);
			$arr = json_decode($rs, true);
			print_r($arr);
			
		}
		
	}
	
	public function actionMenu()
	{
//  		$this->redirect('/');
//  		exit;
		
		$appid = $this->wechat()->appid;
		$secret = $this->wechat()->secret;
		$redirect_uri = urlencode('http://wx.zhishimama.com/qa');
		
		$str = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&#wechat_redirect";
		
		echo $str;exit();
		
		$auth = new Auth($this->wechat()->appid, $this->wechat()->secret);
		
		$accessToken = $auth->accessToken;
		
		$url = 'https://api.weixin.qq.com/cgi-bin/menu/get?access_token='.$accessToken;
		
		$rs = Util::httpGet($url);
		
		$arr = json_decode($rs, true);		
		
		
// 		$arr['menu']['button'][0]['sub_button'][1]['name'] = '社团';
// 		$arr['menu']['button'][0]['sub_button'][1]['url'] = $str;
		
		echo $str;exit;
		
		$arr['menu']['button'][1]['sub_button'][2]['type'] = 'view';
		$arr['menu']['button'][1]['sub_button'][2]['name'] = '社团大本营';
		$arr['menu']['button'][1]['sub_button'][2]['url'] = $str;
		
		
		$updateUrl = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$accessToken;
		
		$newMenu = $arr['menu'];
		
		$menu = Util::jsonEncode($newMenu);
		
		$result = Util::httpPost($updateUrl, $menu);
		
		echo $result;
		
	}
	
	public function actionTongji()
	{
		$sql = 'select u.nickname, log.openid, FROM_UNIXTIME(log.create_time) as log_date from bc_unsubscribe_log as log left join bc_wx_user as u on u.openid = log.openid where log_id > 141 order by log.create_time desc';
		$dao = new \bc\ar\MActiveRecord();
		$list = $dao->getDb()->createCommand($sql)->queryAll();
		foreach ($list as $row)
		{
			$appid = $this->wechat()->appid;
			$secret = $this->wechat()->secret;
				
			$auth = new Auth($appid, $secret);
			$accessToken = $auth->accessToken;
				
			$user = new User();
			$info = $user->getUserInfo($accessToken, $row['openid']);
			echo '用户昵称:'.$info['nickname']. '  ---  取消时间:' . $row['log_date'].'<br/>';
		}
		
		echo '============done=============';
	}
		
	/**
	 *
	 * @return string
	 */
	public function actionError()
	{
		$error = Yii::$app->errorHandler->exception->getMessage ();
		
		return $this->render ( 'error' );
	}
	
	public function actionBroswer()
	{
		$agent = 'unknow';
		echo '<pre>';
		
		$httpUserAgent = $_SERVER['HTTP_USER_AGENT'];
		if($httpUserAgent)
		{
			if(strpos($httpUserAgent, 'MicroMessenger') > 0)
			{
				$agent = 'wechat';
			}
			elseif(strpos($httpUserAgent, 'Weibo') > 0)
			{
				$agent = 'weibo';
			}
		}
		
		
		echo $agent;
		
	}
}
